package api

import (
	"Demo_GoFrame/src/common/response"
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/glog"
)

type addParam struct {
	param1 int
	param2 int
	Result int
}

type DemoApi struct {
	sAddInfo addParam
}

var projectName string = "demo"

func getSelfLog() *glog.Logger {
	return g.Log(projectName)
}

func getDBModel(table string) *gdb.Model {
	return g.DB(projectName).Table(table).Safe()
}

func (selfApi *DemoApi) Add(r *ghttp.Request) {
	paramA := r.GetInt("a", 0)
	paramB := r.GetInt("b", 0)
	selfApi.sAddInfo.param1 = paramA
	selfApi.sAddInfo.param2 = paramB
	selfApi.sAddInfo.Result = paramA + paramB
	response.ToSuccess(r, selfApi.sAddInfo.Result)
}

func (selfApi *DemoApi) GetResult(r *ghttp.Request) {
	response.ToSuccess(r, selfApi.sAddInfo.Result)
}

func (selfApi *DemoApi) GetCameraList(r *ghttp.Request) {
	model := getDBModel("tab_camera")
	result, err := model.All()
	for i := 0; i < len(result); i++ {
		cameraID := result[i]["camera_id"].String()
		result[i]["aaaa"] = g.NewVar(1)
		result[i]["bbb"] = g.NewVar("hello")
		getSelfLog().Info(cameraID)
	}
	if err != nil {
		response.ToFail(r, 9407, "获取滇池活动列表错误!")
		getSelfLog().Error("获取滇池活动列表错误!", err.Error())
	}
	response.ToSuccess(r, result)
}
